namespace Coding4Fun.Maelstrom.Common
{
	public static partial class UrlHelper
	{
		private const string QueryString_NormalizedHealth = "normalizedhealth";
		private const string QueryString_Score = "score";
		private const string QueryString_Damage = "damage";

		private const string QueryString_GameState = "gamestateanimation";

		private const char UriPathSeparator = '/';
		private const char QueryStringSeparator = '&';
		private const char QueryStringMarker = '?';
		private const char QueryStringParamSeparator = '=';

		public static string Score(Player player)
		{
			if (player == Player.Invalid)
				return null;

			return GetBaseUrl(player) +
				   QueryString_Score + QueryStringParamSeparator + "true";
		}

		public static string Damage(Player player, double normalizedHealth)
		{
			if (player == Player.Invalid)
				return null;

			return GetBaseUrl(player) +
					QueryString_Damage + QueryStringParamSeparator + "true" +
					QueryStringSeparator  +
					QueryString_NormalizedHealth + QueryStringParamSeparator + normalizedHealth;
		}

		public static string GameStartedState(Player player)
		{
			return SetGameStateUrl(player, GameStateAnimation.GameStarted);
		}

		public static string LosingState(Player player)
		{
			return SetGameStateUrl(player, GameStateAnimation.GameResultsLost);
		}

		public static string WinningState(Player player)
		{
			return SetGameStateUrl(player, GameStateAnimation.GameResultsWon);
		}

		public static string SetGameStateUrl(Player player, GameStateAnimation state)
		{
			if (player == Player.Invalid)
				return null;

			return GetBaseUrl(player) +
				   QueryString_GameState + QueryStringParamSeparator + ((int) state);
		}

		private static string GetBaseUrl(Player player)
		{
			return "http://" + GetPlayerIp(player) + UriPathSeparator + QueryStringMarker;
		}

		private static string GetPlayerIp(Player player)
		{
			switch (player)
			{
				default:
				case Player.One:
					return Settings.HardwareIp_Player1;
				case Player.Two:
					return Settings.HardwareIp_Player2;
			}
		}
	}
}
